<template>
	<!-- 保证金 -->
	<view class="container" v-if="show">
		<view class="b-top" :style="{ background:isshow?
		    'linear-gradient(180deg, #1D48E1 0%, #469BF6 47%, #F9F9F9 100%)'
			:'linear-gradient(180deg, #FF6F07 0%, #FFA666 47%, #F9F9F9 100%)' }">
			<image src="../../static/bbg.png" mode="aspectFill" class="bg" v-if="isshow"></image>
			<image src="../../static/bbg2.png" mode="aspectFill" class="bg" v-else></image>
			<view class="b-top-ads">
				<view class="t u-flex u-col-center u-row-center" @click="$pageTo(`/pages/user/bondList`)">
					<text>保证金</text><uni-icons type="right" size="16" color="#fff"></uni-icons>
				</view>
				<view class="price">
					<text>{{userInfo.bond}}</text>元
				</view>
				<view class="info u-flex u-col-center u-row-center" @click="open(1)" v-if="isshow">
					<text class="text">商人功能生效</text>
					<uni-icons type="checkbox-filled" size="20" color="#3191ff"></uni-icons>
				</view>
				<view class="info u-flex u-col-center u-row-center" @click="open(2)" v-else>
					<text class="text">保证金不足 功能受限</text>
					<uni-icons type="info" size="20" color="#FF6F07"></uni-icons>
				</view>
				<view class="btn" @click="open(2)" v-if="userInfo.bond < bond">
					立即充值 {{getPrice(userInfo.bond,bond)}}元
				</view>
			</view>
		</view>
		<view class="tb_btns" @click="open3"
		:style="{ top:isshow?'360rpx':'480rpx' }">
			<view class="t-flex">
				<image src="/static/je.png" mode=""></image>
				<view class="">
					申请记录
				</view>
			</view>
			<uni-icons type="right"></uni-icons>
		</view>
		<view class="b-footer"
		 :style="{ marginTop:isshow?'-40rpx':'80rpx' }">
			<view class="t">
				什么是保证金？
			</view>
			<view class="body">
				<view class="" v-html="content"></view>
			</view>
		</view>
	<!-- 	<view class="tb" @click="open2">
			申请退保
		</view>
		<view class="tb1" @click="open3">
			申请记录
		</view> -->
		<view class="" style="height: 260rpx;"></view>
		<view class="footer_btn">
			<view class="cont" @click="open2">
				<view class="btn " :class="isshow?'bt1':''">
					申请退保
				</view>
			</view>
		</view>
		<uni-popup ref="popup" type="center">
			<view class="m-pop">
				<image src="../../static/pop1.png" mode="aspectFill" v-if="tips==1"></image>
				<image src="../../static/pop2.png" mode="aspectFill" v-else></image>
				<view class="t">
					{{tips==1?"您的保证金充足，商人功能已生效":"保证金不足时，商人功能将受限"}}
				</view>
				<view class="btn" @click="close" v-if="tips==1">
					我知道了
				</view>
				<view class="btn on" v-else @click="pay">
					<view class="" v-if="isios">
						ios 暂不支持购买
					</view>
					<view class="" v-else>
						立即充值 {{getPrice(userInfo.bond,bond)}}元
					</view>
					
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popup1" type="center">
			<view class="m-pop2">
				<view class="t" v-if="refund">
					是否确认申请保证金？
				</view>
				<view class="t" v-else>
					当前存在发布中的报价<br>请将结束报价后再退保
				</view>
				<view class="btns u-flex u-col-center u-row-between" v-if="refund">
					<view class="btn btn1" @click="tuibao">
						确认
					</view>
					<view class="btn btn2" @click="close2">
						取消
					</view>
				</view>
				<view class="btns u-flex u-col-center u-row-between" v-else>
					<view class="btn btn1" @click="close2">
						取消
					</view>
					<view class="btn btn2" @click="$pageTo('/pages/user/order/orderList')">
						去查看
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getUserinfo,
		webconfig,
		bondRecharge,
		queryCount
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				isios:uni.getStorageSync('isios'),
				refund: true,
				tips: 1,
				content: "",
				bond: "",
				userInfo: {
					bond: 0
				},
				isshow:false,
				show:false
			}
		},
		onLoad() {
			this.getwebconfig()
			queryCount({}).then(res=>{
				if(res.code == 200){
					this.refund = res.data.refund
				}
			})
		},
		computed: {
			getPrice() {
				return (bond1, bond2) => {
					return parseFloat(bond2) - parseFloat(bond1)
				}
			},
		},
		methods: {
			getwebconfig() {
				webconfig({}).then(res => {
					if (res.code == 200) {
						this.content = res.data.bondrule
						this.bond = res.data.bond
						this.getUser()
					}
				})
			},
			getUser() {
				getUserinfo({}).then(res => {
					if (res.code == 200) {
						this.userInfo = res.data
						uni.setNavigationBarColor({
							backgroundColor: '#ffffff',
							frontColor: "#ffffff"
						})
						if (this.userInfo.bond >= this.bond) {
							uni.setNavigationBarColor({
								backgroundColor: '#1D48E1',
								frontColor: "#ffffff"
							})
							this.isshow = true
							this.show = true
						} else {
							uni.setNavigationBarColor({
								backgroundColor: '#FF6F07',
								frontColor: "#ffffff"
							})
							this.isshow = false
							this.show = true
						}
					}
				})
			},
			open(i) {
				this.tips = i
				this.$refs.popup.open()
			},
			open2() {
				this.$refs.popup1.open()
			},
			open3(){
				uni.navigateTo({
					url:'/pages/user/applytbList'
				})
			},
			close() {
				this.$refs.popup.close()
			},
			close2() {
				this.$refs.popup1.close()
			},
			tuibao() {
				this.$refs.popup1.close()
				uni.navigateTo({
					url:'/pages/user/applytb'
				})
			},
			pay() {
				if(this.isios){
					uni.showToast({
						icon: "none",
						title: "ios 暂不支持购买",
					});
					return
				}
				let money = parseFloat(this.bond) - parseFloat(this.userInfo.bond)
				bondRecharge({
					bond: parseFloat(money),
					price: parseFloat(money),
					paytype: 1,
				}).then(res => {
					if (res.code == 200) {
						let byteAuthorization = res.data.byteAuthorization
						let data = res.data.data
						tt.requestOrder({
							data: JSON.stringify(data), // 开发者服务端返回的data示例
							byteAuthorization: byteAuthorization, // 开发者服务端返回的byteAuthorization
							success: (res) => {
								const {
									orderId
								} = res;
								console.log("orderId", orderId); // 抖音开放平台内部的交易订单号
								tt.getOrderPayment({
									orderId: orderId, // 替换为抖音开放平台内部的交易订单号
									success: (res) => {
										console.log("支付成功", res);
										tt.showToast({
											icon: "success",
											title: "支付成功",
										});
										setTimeout(() => {
											uni.switchTab({
												url: '/pages/user/user'
											})
										}, 500)
									},
									fail: (res) => {
										console.error("支付失败", res);
										const {
											errNo,
											errMsg
										} = res;
										tt.showToast({
											icon: "fail",
											title: "支付失败",
										});
									},
								});
							},
							fail: (res) => {
								const {
									errLogId,
									errMsg,
									errNo
								} = res;
								console.log("errNo:", res);
								console.log("errNo:", errNo);
								console.log("errMsg:", errMsg);
							},
						});
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		position: relative;
		.b-top {
			height: 520rpx;
			position: relative;
			background: #fff;
			.bg {
				width: 100%;
				height: 270rpx;
			}

			.b-top-ads {
				position: absolute;
				width: 100%;
				height: 520rpx;
				top: 0%;
			}

			.btn {
				width: 690rpx;
				height: 100rpx;
				line-height: 100rpx;
				background: linear-gradient(270deg, #FF6F07 0%, #FFA666 100%);
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				margin: 40rpx auto;
				text-align: center;
				font-size: 32rpx;
				color: #fff;
				font-weight: 600;
			}

			.t {
				width: 200rpx;
				height: 50rpx;
				line-height: 50rpx;
				background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.3) 47%, rgba(255, 255, 255, 0) 100%);
				text-align: center;
				margin: 40rpx auto 30rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #FFFFFF;

				text {
					margin-right: 10rpx;
				}
			}

			.price {
				text-align: center;
				height: 130rpx;
				line-height: 130rpx;
				font-size: 28rpx;
				color: #FFFFFF;

				text {
					font-family: "DINB";
					font-weight: bold;
					font-size: 120rpx;
					margin-right: 10rpx;
				}
			}

			.info {
				margin-top: 20rpx;
				font-weight: 500;
				font-size: 28rpx;
				color: #FFFFFF;
				text-align: center;

				.text {
					margin-right: 10rpx;
					height: 50rpx;
					line-height: 50rpx;
					display: inline-block;
				}
			}
		}
		.tb_btns{
			position: absolute;
			left: 30rpx;
			width: 690rpx;
			height: 100rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0rpx 20rpx;
			box-sizing: border-box;
			.t-flex{
				display: flex;
				align-items: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #11192D;
			}
			image{
				width: 44rpx;
				height: 44rpx;
				margin-right: 16rpx;
			}
		}

		.b-footer {
			position: relative;
			margin:0rpx auto;
			background-color: #fff;
			padding: 30rpx;
			border-radius: 20rpx;
			width: 690rpx;
			box-sizing: border-box;
			.t {
				line-height: 40rpx;
				height: 40rpx;
				font-weight: 500;
				font-size: 32rpx;
				color: #11192D;
				text-align: left;
			}

			.body {
				margin-top: 20rpx;
				line-height: 40rpx;
				font-size: 28rpx;
				color: #979797;
			}
		}

		.tb {
			width: 300rpx;
			height: 80rpx;
			background: #fff;
			border-radius: 12rpx;
			line-height: 80rpx;
			position: fixed;
			bottom: 150rpx;
			text-align: center;
			font-size: 32rpx;
			color: #C3C3C3;
			left: 50%;
			transform: translate(-50%);
		}

		.tb1 {
			width: 300rpx;
			text-decoration-line: underline;
			border-radius: 12rpx;
			position: fixed;
			bottom: 80rpx;
			text-align: center;
			font-size: 32rpx;
			color: #C3C3C3;
			left: 50%;
			transform: translate(-50%);
		}


		.m-pop2 {
			width: 620rpx;
			height: 340rpx;
			position: relative;
			display: flex;
			align-items: center;
			justify-content: center;
			text-align: center;
			flex-direction: column;
			box-sizing: border-box;
			padding: 60rpx 40rpx 20rpx;
			background: #fff;
			border-radius: 20rpx;

			.t {
				line-height: 44rpx;
				font-size: 32rpx;
				color: #000000;
				text-align: center;
			}

			.btns {
				width: 100%;
				margin-top: 50rpx;

				.btn {
					text-align: center;
					font-size: 32rpx;
					width: calc(50% - 10rpx);
					height: 90rpx;
					line-height: 90rpx;
					border-radius: 20rpx;
				}

				.btn1 {

					background: #F9F9F9;
					color: #D1CDCD;
				}

				.btn2 {
					color: #fff;
					background: linear-gradient(270deg, #FF6F07 0%, #FFA666 100%);

				}
			}
		}

		.m-pop {
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			text-align: center;
			width: 620rpx;
			padding: 80rpx 0 40rpx;
			background: #fff;
			border-radius: 20rpx;

			image {
				width: 144rpx;
				height: 144rpx;
			}

			.t {
				line-height: 40rpx;
				margin-top: 60rpx;
				height: 40rpx;
				font-weight: 500;
				font-size: 32rpx;
				color: #000000;
				text-align: center;

			}

			.btn {
				width: 440rpx;
				height: 90rpx;
				line-height: 90rpx;
				background-color: #F9F9F9;
				border-radius: 20rpx;
				font-size: 28rpx;

				margin-top: 50rpx;
				color: #D1D1D6;
			}

			.on {
				background: linear-gradient(270deg, #FF6F07 0%, #FFA666 100%);
				color: #fff;
			}
		}
	}
</style>